﻿Imports System.Xml
Imports System.IO
Imports System.Data
Imports System.Text
Public Class ClsProtocole
    Private _strProtName As String
    Private _strProtField1Name As String
    Private _strProtField1Type As String
    Private _strProtField1Unit As String
    Private _strProtField1Value As String
    Private _strProtField2Name As String
    Private _strProtField2Type As String
    Private _strProtField2Unit As String
    Private _strProtField2Value As String
    Private _strProtListValues As String



    Public Function ExportProtToXML(ByRef writer As XmlWriter) As XmlWriter
        Dim i, j As Short
        Try
            writer.WriteStartElement("PROTOCOLE")
            writer.WriteAttributeString("Name", "", Me._strProtName)
            '   writer.WriteElementString("NAME", Me._strProtName)
            writer.WriteElementString("FIELD1_NAME", Me._strProtField1Name)
            writer.WriteElementString("FIELD1_TYPE", Me._strProtField1Type)
            writer.WriteElementString("FIELD1_UNIT", Me._strProtField1Unit)
            writer.WriteElementString("FIELD1_VALUE", Me._strProtField1Value)
            writer.WriteElementString("FIELD2_NAME", Me._strProtField2Name)
            writer.WriteElementString("FIELD2_TYPE", Me._strProtField2Type)
            writer.WriteElementString("FIELD2_UNIT", Me._strProtField2Unit)
            writer.WriteElementString("FIELD2_VALUE", Me._strProtField2Value)

            If Me._strProtField1Type <> "liste" Then
                writer.WriteElementString("LIST", Me._strProtListValues)
            Else
                writer.WriteStartElement("LIST")
                i = Me._strProtListValues.Split(";")(0)
                For j = 2 To i + 1
                    writer.WriteElementString("ITEM", Me._strProtListValues.Split(";")(j))
                Next
                writer.WriteEndElement()
            End If

            writer.WriteEndElement()
            Return writer
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public Function ExportProtocoleToXML() As String
        Try
            Dim output As New StringWriter()
            Dim xsettings As New XmlWriterSettings()
            xsettings.Indent = True
            xsettings.NewLineOnAttributes = True
            xsettings.Encoding = New UTF8Encoding(False)
            Dim XmlWriter As XmlWriter = XmlWriter.Create(output, xsettings)
            XmlWriter.WriteProcessingInstruction("xml", "version=""1.0"" encoding=""utf-8"" standalone=""yes""")
            XmlWriter.WriteStartElement("PROTOCOLES")
            ' For Each attrib As Attribute In oProtocole
            XmlWriter = ExportProtToXML(XmlWriter)
            ' Next
            XmlWriter.WriteEndElement()
            XmlWriter.WriteEndDocument()
            XmlWriter.Flush()
            Return output.ToString()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function


    '##########################################################################################################################
    '                       CONSTRUCTEURS et Cie
    '##########################################################################################################################
    Public Sub New()
        Me._strProtName = "Null"
        Me._strProtField1Name = "Null"
        Me._strProtField1Type = "Null"
        Me._strProtField1Unit = "Null"
        Me._strProtField1Value = "Null"
        Me._strProtField2Name = "Null"
        Me._strProtField2Type = "Null"
        Me._strProtField2Unit = "Null"
        Me._strProtField2Value = "Null"
        Me._strProtListValues = "Null"
    End Sub

    Public Sub New(ByVal ProtName As String, ByVal Field1Name As String, ByVal Field1Type As String, ByVal Field1Unit As String, _
                   ByVal Field1Value As String, Optional ByVal Field2Name As String = "Null", Optional ByVal Field2Type As String = "Null", Optional ByVal Field2Unit As String = "Null", Optional ByVal Field2Value As String = "Null", Optional ByVal ListValues As String = "Null")

        Me.Init(ProtName, Field1Name, Field1Type, Field1Unit, Field1Value, Field2Name, Field2Type, Field2Unit, Field2Value, ListValues)

    End Sub

    Sub Init(ByVal ProtName As String, ByVal Field1Name As String, ByVal Field1Type As String, ByVal Field1Unit As String, _
                   ByVal Field1Value As String, Optional ByVal Field2Name As String = "Null", Optional ByVal Field2Type As String = "Null", Optional ByVal Field2Unit As String = "Null", Optional ByVal Field2Value As String = "Null", Optional ByVal ListValues As String = "Null")


        Me._strProtName = ProtName
        Me._strProtField1Name = Field1Name
        Me._strProtField1Type = Field1Type
        Me._strProtField1Unit = Field1Unit
        Me._strProtField1Value = Field1Value
        Me._strProtField2Name = Field2Name
        Me._strProtField2Type = Field2Type
        Me._strProtField2Unit = Field2Unit
        Me._strProtField2Value = Field2Value
        Me._strProtListValues = ListValues

    End Sub


    Public Sub New(ByVal XmlFile As String, Optional ByVal nodeName As String = "Null")
        Dim XmlDoc As XmlDocument = New Xml.XmlDocument()

        Try

            XmlDoc.Load(XmlFile)
            Dim root As XmlElement = XmlDoc.DocumentElement
            Dim oXmlNode, oXmlNode2 As XmlNode
            Dim nodeList, nodeList2 As XmlNodeList
            Dim strProtocole, strNameProt As String
            Dim i As Short
            Dim strItemsListe, strItemsListe2 As String
            strNameProt = ""
            nodeList = Nothing
            nodeList2 = Nothing
            oXmlNode = Nothing
            

            '- <PROTOCOLE Name="Individus">
            '<FIELD1_NAME>Nombre</FIELD1_NAME> 
            '<FIELD1_TYPE>entier</FIELD1_TYPE> 
            '<FIELD1_UNIT /> 
            '<FIELD1_VALUE>0</FIELD1_VALUE> 
            '<FIELD2_NAME>Null</FIELD2_NAME> 
            '<FIELD2_TYPE>Null</FIELD2_TYPE> 
            '<FIELD2_UNIT>Null</FIELD2_UNIT> 
            '<FIELD2_VALUE>Null</FIELD2_VALUE> 
            '</PROTOCOLE>

            If (nodeName = "Null") Then

                oXmlNode = root.FirstChild
                nodeList = oXmlNode.ChildNodes
                strNameProt = oXmlNode.Attributes.Item(0).Value & ";"

            ElseIf nodeName <> "" Then


                oXmlNode = root.SelectSingleNode("PROTOCOLE[@Name= '" & nodeName & "' ]")
                nodeList = oXmlNode.ChildNodes
                strNameProt = oXmlNode.Attributes.Item(0).Value

            End If
            ' construction de l'objet oProtocole


            strNameProt = oXmlNode.Attributes.Item(0).Value
            oXmlNode2 = root.SelectSingleNode("PROTOCOLE/FIELD1_TYPE")

            If oXmlNode2.InnerText = "liste" Then
                ' recuperer les items de la liste dans une variable

                oXmlNode2 = root.SelectSingleNode("PROTOCOLE/LIST")
                nodeList2 = oXmlNode2.ChildNodes
                i = nodeList2.Count
                strItemsListe = ""
                Dim intLong As Short = 0
                For Each oXmlNode In nodeList2
                    Dim strValeur As String
                    strValeur = oXmlNode.InnerText
                    ' recuperer la longueur max des mots des items pour generer le bouton
                    intLong = Math.Max(intLong, strValeur.Length)
                    strItemsListe = strItemsListe & strValeur & ";"
                Next
                strItemsListe2 = i & ";" & intLong & ";" & strItemsListe
            Else
                strItemsListe2 = "Null"
            End If

            strProtocole = nodeList.Item(0).InnerText & nodeList.Item(1).InnerText & nodeList.Item(2).InnerText & nodeList.Item(3).InnerText _
            & nodeList.Item(4).InnerText & nodeList.Item(5).InnerText & nodeList.Item(6).InnerText & nodeList.Item(7).InnerText

            'Me.Init(strProtocole.Split(";")(0), _
            '            strProtocole.Split(";")(1), _
            '            strProtocole.Split(";")(2), _
            '            strProtocole.Split(";")(3), _
            '            strProtocole.Split(";")(4), _
            '            strProtocole.Split(";")(5), _
            '            strProtocole.Split(";")(6), _
            '            strProtocole.Split(";")(7), _
            '            strProtocole.Split(";")(8))

            Me.Init(strNameProt, _
                            nodeList.Item(0).InnerText, _
                            nodeList.Item(1).InnerText, _
                            nodeList.Item(2).InnerText, _
                            nodeList.Item(3).InnerText, _
                            nodeList.Item(4).InnerText, _
                            nodeList.Item(5).InnerText, _
                            nodeList.Item(6).InnerText, _
                            nodeList.Item(7).InnerText, strItemsListe2)

        Catch ex As Exception
            Throw New Exception("Erreur chargement fichier Protocole: " & ex.Message)
        End Try

    End Sub


    '##########################################################################################################################
    '                       ACCESSEURS
    '##########################################################################################################################

    Public Property strProtName() As String
        Get
            Return _strProtName
        End Get
        Set(ByVal value As String)
            _strProtName = value
        End Set
    End Property

    Public Property strProtField1Name() As String
        Get
            Return _strProtField1Name
        End Get
        Set(ByVal value As String)
            _strProtField1Name = value
        End Set
    End Property
    Public Property strProtField1Type() As String
        Get
            Return _strProtField1Type
        End Get
        Set(ByVal value As String)
            _strProtField1Type = value
        End Set
    End Property

    Public Property strProtField1Unit() As String
        Get
            Return _strProtField1Unit
        End Get
        Set(ByVal value As String)
            _strProtField1Unit = value
        End Set
    End Property

    Public Property strProtField1Value() As String
        Get
            Return _strProtField1Value
        End Get
        Set(ByVal value As String)
            _strProtField1Value = value
        End Set
    End Property
    Public Property strProtField2Name() As String
        Get
            Return _strProtField2Name
        End Get
        Set(ByVal value As String)
            _strProtField2Name = value
        End Set
    End Property

    Public Property strProtField2Type() As String
        Get
            Return _strProtField2Type
        End Get
        Set(ByVal value As String)
            _strProtField2Type = value
        End Set
    End Property
    Public Property strProtField2Unit() As String
        Get
            Return _strProtField2Unit
        End Get
        Set(ByVal value As String)
            _strProtField2Unit = value
        End Set
    End Property

    Public Property strProtField2Value() As String
        Get
            Return _strProtField2Value
        End Get
        Set(ByVal value As String)
            _strProtField2Value = value
        End Set
    End Property

    Public Property strProtListValues() As String
        Get
            Return _strProtListValues
        End Get
        Set(ByVal value As String)
            _strProtListValues = value
        End Set
    End Property

    
End Class
